package com.geostar.constructionland.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geostar.constructionland.index.entity.IndexUnfreezeRecordEntity;
import com.geostar.constructionland.index.entity.InxOutStorageAcotEntity;
import com.geostar.constructionland.index.entity.model.InxOutStorageUnFreezeModel;
import com.geostar.constructionland.index.mapper.IndexUnfreezeRecordMapper;
import com.geostar.constructionland.index.util.FormatUtils;
import com.geostar.constructionland.index.entity.vo.SelInxOutStorageAcotData;
import com.geostar.constructionland.index.entity.model.InxOutStogAcotModel;
import com.geostar.constructionland.index.mapper.DeleteInxOutStorageAcotMapper;
import com.geostar.constructionland.index.mapper.InxOutStorageAcotMapper;
import com.geostar.constructionland.index.service.InxOutStorageAcotService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class InxOutStogAcotSerImpl implements InxOutStorageAcotService {

    @Resource
    InxOutStorageAcotMapper inxOutStorageAcotMapper;
    @Resource
    DeleteInxOutStorageAcotMapper deleteInxOutStorageAcotMapper;
    @Resource
    IndexUnfreezeRecordMapper indexUnfreezeRecordMapper;

    /**
     * 出库台账分页查询
     * @param inxOutStogAcotModel
     * @return
     */
    @Override
    public IPage<SelInxOutStorageAcotData> queryInxOutStogAcotList(InxOutStogAcotModel inxOutStogAcotModel) {

        //设置查询条件（利用条件构造器）
        QueryWrapper<SelInxOutStorageAcotData> queryWrapper = getQueryWrapper(inxOutStogAcotModel);
        //查询数据
        return inxOutStorageAcotMapper.queryInxOutStorageAcotByWrapper(new Page<>(inxOutStogAcotModel.getPagenum(),inxOutStogAcotModel.getPagesize()),
                queryWrapper);
    }

    @Override
    public List<SelInxOutStorageAcotData> queryInxStorageAllList(InxOutStogAcotModel inxOutStogAcotModel) {

        //设置查询条件（利用条件构造器）
        QueryWrapper<SelInxOutStorageAcotData> queryWrapper = getQueryWrapper(inxOutStogAcotModel);
        //查询数据
        return inxOutStorageAcotMapper.queryInxOutStorageAcotByWrapper(queryWrapper);
    }

    /**
     *  设置出库台账查询条件
     * @param inxOutStogAcotModel
     * @return
     */
    private QueryWrapper<SelInxOutStorageAcotData> getQueryWrapper(InxOutStogAcotModel inxOutStogAcotModel) {
        //设置查询条件（利用条件构造器）
        QueryWrapper<SelInxOutStorageAcotData> queryWrapper = new QueryWrapper<>();
        //行政区划代码-省（出库台账查询条件）
        if(!StringUtils.isBlank(inxOutStogAcotModel.getDistCodeProvince())){
            queryWrapper.eq("distCodeProvince",inxOutStogAcotModel.getDistCodeProvince());
        }
        //行政区划代码-市（出库台账查询条件）
        if(!StringUtils.isBlank(inxOutStogAcotModel.getDistCodeCity())){
            queryWrapper.eq("distCodeCity",inxOutStogAcotModel.getDistCodeCity());
        }
        //行政区划代码-县（出库台账查询条件）
        if(!StringUtils.isBlank(inxOutStogAcotModel.getDistCodeCountry())){
            queryWrapper.eq("distCodeCountry",inxOutStogAcotModel.getDistCodeCountry());
        }
        //指标年份（出库台账查询条件）
        if(!StringUtils.isBlank(inxOutStogAcotModel.getIndexYear())){
            queryWrapper.eq("indexYear",inxOutStogAcotModel.getIndexYear());
        }
        //出库状态（出库台账查询条件）
        if(!StringUtils.isBlank(inxOutStogAcotModel.getOutStorageStatus())){
            queryWrapper.in("outStorageStatus",inxOutStogAcotModel.getOutStorageStatus());
        }
        //入库id（出库台账查询条件）
        if(!StringUtils.isBlank(inxOutStogAcotModel.getInxStorageId())){
            queryWrapper.eq("inxStorageId",inxOutStogAcotModel.getInxStorageId() );
        }
        //指标类型（出库台账查询条件）
        if(!StringUtils.isBlank(inxOutStogAcotModel.getIndexType())){
            queryWrapper.in("indexType", FormatUtils.getStringByList(inxOutStogAcotModel.getIndexType()));
        }
        //项目名称（出库台账查询条件）
        if(!StringUtils.isBlank(inxOutStogAcotModel.getProjectName())){
            queryWrapper.like("projectName",inxOutStogAcotModel.getProjectName());
        }
        //业务类型（出库台账查询条件）
        if(!StringUtils.isBlank(inxOutStogAcotModel.getBusinessType())){
            queryWrapper.in("businessType", FormatUtils.getStringByList(inxOutStogAcotModel.getBusinessType()));
        }
        //出库类型（出库台账查询条件）
        if(!StringUtils.isBlank(inxOutStogAcotModel.getOutStorageType())){
            queryWrapper.in("outStorageType", FormatUtils.getStringByList(inxOutStogAcotModel.getOutStorageType()));
        }
        //排序
        if(!StringUtils.isBlank(inxOutStogAcotModel.getSortName())){
            if(!StringUtils.isBlank(inxOutStogAcotModel.getSortOrder())){
                if (inxOutStogAcotModel.getSortOrder().equals("desc")){
                    queryWrapper.orderByDesc(inxOutStogAcotModel.getSortName());
                }else{
                    queryWrapper.orderByAsc(inxOutStogAcotModel.getSortName());
                }
            }
        }
        return queryWrapper;
    }

    @Override
    @Transactional
    public String inxOutStorageUnFreeze(InxOutStorageUnFreezeModel inxOutStorageUnFreezeModal){
        //查询当前数据库保存的冻结指标（出库台账，根据出库id）
        InxOutStorageAcotEntity currentInxOutStorageAcot =inxOutStorageAcotMapper.selectById(inxOutStorageUnFreezeModal.getInxOutStorageId());
        //若查询出来的不是冻结指标，则提示错误
        if(!"08".equals(currentInxOutStorageAcot.getOutStorageStatus())) {
            return "Message:非冻结指标无法保存";
        }
        //计算冻结后的指标（解冻后的指标=解冻前的指标-解冻的指标）
        InxOutStorageAcotEntity inxOutStorageAcot = new InxOutStorageAcotEntity();
        //新增建设用地指标（解冻后）
        inxOutStorageAcot.setBuildLandAdd(currentInxOutStorageAcot.getBuildLandAdd().subtract(inxOutStorageUnFreezeModal.getBuildLandAdd()));
        //农用地（解冻后）
        inxOutStorageAcot.setAgclLand(currentInxOutStorageAcot.getAgclLand().subtract(inxOutStorageUnFreezeModal.getAgclLand()));
        //耕地（解冻后）
        inxOutStorageAcot.setFarmland(currentInxOutStorageAcot.getFarmland().subtract(inxOutStorageUnFreezeModal.getFarmland()));
        //修改出库台账数据指标数据(update jsyd_zbcktz set gd=#gd_post#,nyd=#nyd_post#,xzjsyd=#xzjsyd_post# where ckid=#ckid# and ckzt = '08'
        UpdateWrapper<InxOutStorageAcotEntity> updateWrapper = new UpdateWrapper<InxOutStorageAcotEntity>();
        updateWrapper.eq("ckzt", "08");
        updateWrapper.eq("ckid",inxOutStorageUnFreezeModal.getInxOutStorageId());
        inxOutStorageAcotMapper.update(inxOutStorageAcot, updateWrapper);
        //保存指标冻结记录
        IndexUnfreezeRecordEntity indexUnfreezeRecordEntity = new IndexUnfreezeRecordEntity();
        indexUnfreezeRecordEntity.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        indexUnfreezeRecordEntity.setInxOutStorageAcotId(inxOutStorageUnFreezeModal.getInxOutStorageId());
        //冻结的指标
        indexUnfreezeRecordEntity.setBuildLandAdd(inxOutStorageUnFreezeModal.getBuildLandAdd());
        indexUnfreezeRecordEntity.setAgclLand(inxOutStorageUnFreezeModal.getAgclLand());
        indexUnfreezeRecordEntity.setFarmland(inxOutStorageUnFreezeModal.getFarmland());
        //冻结前的指标
        indexUnfreezeRecordEntity.setBuildLandAddPre(currentInxOutStorageAcot.getBuildLandAdd());
        indexUnfreezeRecordEntity.setAgclLandPre(currentInxOutStorageAcot.getAgclLand());
        indexUnfreezeRecordEntity.setFarmlandPre(currentInxOutStorageAcot.getFarmland());
        indexUnfreezeRecordEntity.setBusinessId(currentInxOutStorageAcot.getBusinessId());
        indexUnfreezeRecordEntity.setUSERID(inxOutStorageUnFreezeModal.getOperatorId());
        indexUnfreezeRecordEntity.setUSERNAME(inxOutStorageUnFreezeModal.getOperatorName());
        indexUnfreezeRecordEntity.setInxStorageAcotId(currentInxOutStorageAcot.getInxStorageIAcotId());
        indexUnfreezeRecordEntity.setRemark(inxOutStorageUnFreezeModal.getRemark());
        indexUnfreezeRecordMapper.insert(indexUnfreezeRecordEntity);
        return "Message:success";
    }


}
